#!/bin/bash
#save the data
cp -r jupyter/root ~ 2>/dev/null

#enter home and create jupyter dir
cd $HOME & rm -rf jupyter && mkdir jupyter
cd jupyter

#make sure python3 is installed
#create virtualenv and install jupyter
python3 -m venv venv
source venv/bin/activate
pip install jupyter

#create project rootdir
mkdir root

#create pw
pw=$(python3 -c "from notebook.auth import passwd;print(passwd())")


#create config
jupyter notebook --generate-config --allow-root -y
cat >> $HOME/.jupyter/jupyter_notebook_config.py<<-EOF
c.NotebookApp.ip = '*'
c.NotebookApp.password = u'$pw'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8888
c.NotebookApp.allow_root = True
c.NotebookApp.notebook_dir = u'$HOME/jupyter/root'
EOF

#other extensions
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user

#create links
rm -rf /usr/local/bin/jupyter
ln -s $HOME/jupyter/venv/bin/jupyter /usr/local/bin/jupyter

#recover the data
cp -r ~/root jupyter/ 2>/dev/null

# some fix
pip install nbconvert==5.6.1
pip install jupyter-client==6.1.12

#install analysis packages (seaborn includes numpy pandas matplotlib scipy)
pip install openpyxl xlrd seaborn statsmodels pyecharts plotly mlxtend

#create service
systemctl stop jupyter
cat >/usr/lib/systemd/system/jupyter.service<<-EOF
[Unit]
Description=jupyter

[Service]
ExecStart=$HOME/jupyter/venv/bin/jupyter notebook

[Install]
WantedBy=multi-user.target
EOF
systemctl enable jupyter
systemctl daemon-reload
systemctl start jupyter
